package com.kgcx.organization.service;

import com.github.pagehelper.PageHelper;
import com.kgcx.common.bean.RequestData;
import com.kgcx.common.constant.Cons;
import com.kgcx.common.utils.TokenUtils;
import com.kgcx.dbutils.domain.BaseDomain;
import com.kgcx.dbutils.utils.PageUtils;
import com.kgcx.organization.domain.Customer;
import com.kgcx.organization.mapper.CustomerMapper;
import com.kgcx.organization.rabbitmq.producer.CustomerProducer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

@Service
public class CustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private CustomerProducer customerProducer;


    public Customer getCustomer(RequestData rd) {
        Customer customer = new Customer();
        customer.setKid(rd.getLong("kid"));
        if (StringUtils.isNotBlank(rd.getString("token"))) {
            customer.setKid(TokenUtils.getId(rd.getString("token")));
        }
        customer.setPhone(rd.getString("phone"));
        customer.setWxId(rd.getString("wxid"));
        return customerMapper.selectOneActive(customer);
    }

    @Transactional(rollbackFor = Exception.class)
    public void addCustomer(RequestData rd) {
        Customer customer = new Customer();
        customer.setPhone(rd.getString("phone"));
        customer.setWxId(rd.getString("wxid"));
        customer.setSource(rd.getInteger("source"));
        customer.setName("");
        customer.setGender(Customer.GENDER_UNKNOWN);
        customer.setMac(rd.getString("mac"));
        customer.setImei(rd.getString("imei"));
        customer.setImsi(rd.getString("imsi"));
        customer.setIp(rd.getString("ip"));
        customer.setPort(rd.getString("port"));
        Timestamp now = new Timestamp(System.currentTimeMillis());
        customer.setCreateTime(now);
        customer.setUpdateTime(now);
        customer.setStatus(Cons.COMMON_STATUS_ACTIVE);
        customerMapper.insertSelective(customer);

        customerProducer.addCustomer(customer);
    }

    @Transactional(rollbackFor = Exception.class)
    public Customer loginOrRegister(RequestData rd) {
        Customer customer = getCustomer(rd);
        if (null == customer) {
            addCustomer(rd);
            customer = getCustomer(rd);
        }
        if (null != customer) {
            customer.setLat(rd.getString("lat"));
            customer.setLng(rd.getString("lng"));
            customer.setMac(rd.getString("mac"));
            customer.setImei(rd.getString("imei"));
            customer.setImsi(rd.getString("imsi"));
            customer.setIp(rd.getString("ip"));
            customer.setPort(rd.getString("port"));
            customerProducer.loginCustomer(customer);
        }
        return customer;
    }

    /**
     * 提供监管平台查询乘客信息
     *
     * @param rd
     * @return
     */
    public List<Customer> queryCustomerList(RequestData rd) {
        PageUtils.page(rd);
        PageHelper.orderBy(rd.getOrderBy());
        return customerMapper.selectActive(BaseDomain.buildSelectDomain(rd, new Customer()));

    }

    /**
     * 提供监管平台查询乘客信息
     *
     * @param rd
     * @return
     */
    public List<Customer> queryCustomerWithPage(RequestData rd) {
        List<Customer> list = queryCustomerList(rd);
        //发生消息给MQ
        for (Customer cust : list) {
            customerProducer.addCustomer(cust);
        }
        return list;

    }
}
